package com.example.demo.hcontroller;

import com.example.demo.pojo.Score;
import com.example.demo.pojo.Tpaper;
import com.example.demo.service.IScoreService;
import com.example.demo.service.ITpaperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@Controller
@RequestMapping(value = "/hTpaper")
public class HTpaperController {

    @Autowired
    private ITpaperService TpaperService;

    @Autowired
    private IScoreService ScoreService;
    @RequestMapping("/toTpaper")
    private String toTpaper(Model model, Tpaper Tpaper) {
        List<Tpaper> list = TpaperService.selectTpaper();
        model.addAttribute("tpaperlist", list);
        List<Score> slist = ScoreService.selectScoreByTid();
        model.addAttribute("scorelist", slist);
        return "tpaper";
    }

    @RequestMapping("/toaddTpaper")
    private String toaddTpaper(Model model, Tpaper Tpaper) {
        return "addtpaper";
    }

    @RequestMapping("/addTpaper")
    private String addTpaper(@RequestParam(value = "file") MultipartFile file, Model model, Tpaper Tpaper, String name, String tag, String brief) throws IOException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(df.format(new Date()));
        System.out.println("file:"+file);
        String path1 = System.getProperty("user.dir");//项目根目录
        String filePath =path1+ "\\src\\main\\resources\\static\\images";
        String originalFilename = file.getOriginalFilename();
        System.out.println("originalFilename:" + originalFilename);
        String newFileName = UUID.randomUUID() + originalFilename;
        File targetFile = new File(filePath, newFileName);
        file.transferTo(targetFile);
        Tpaper.setImg("http://localhost:8080/images/" + newFileName);
        Tpaper.setName(name);
        Tpaper.setTime(df.format(new Date()));
        Tpaper.setTag(tag);
        Tpaper.setBrief(brief);
        TpaperService.insertSelective(Tpaper);
        return "redirect:toTpaper";
    }

    @RequestMapping("/toupdateTpaper")
    private String toupdateTpaper(Model model, Tpaper Tpaper, Integer id) {
        model.addAttribute("tpaper", TpaperService.selectByPrimaryKey(id));
        return "updatetpaper";
    }

    @RequestMapping("/updateTpaper")
    private String updateTpaper(Tpaper Tpaper, Integer id, String name, String tag, String brief) {
        Tpaper.setName(name);
        Tpaper.setId(id);
        Tpaper.setTag(tag);
        Tpaper.setBrief(brief);
        TpaperService.updateByPrimaryKeySelective(Tpaper);
        return "redirect:toTpaper";
    }

    @RequestMapping("/deleteTpaper")
    private String deleteTpaper(Integer id) {
        TpaperService.deleteByPrimaryKey(id);
        return "redirect:toTpaper";
    }
}
